/*
 * ImageRecorder.cpp
 *
 *  Created on: 12.06.2014
 *      Author: philipp
 */

#include "ImageRecorder.hpp"

static void displayUI(const std::string &name, const std::string &path) {
	cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
	int imageId = 0;

	for (;;) {
		cv::Mat picture = Camera::getInstance().takeImage();
		cv::putText(picture, "Press any key to take an image. Use ESC to quit.",
				cv::Point(20, 20), cv::FONT_HERSHEY_PLAIN, 1,
				cv::Scalar(255, 0, 0));
		cv::imshow("Display window", picture);

		int pressedKey = cv::waitKey(50);

		if (pressedKey == 27) { //the esc key has been pressed
			//exit program
			std::cout << "Bye!" << std::endl;
			exit(0);
		}

		if (pressedKey != -1) {
			//any key was pressed.
			FileHelper::saveScreenshot(
					Camera::getInstance().takeFormattedImage(),
					path + "/" + name + "_" + std::to_string(++imageId)
							+ ".jpg", true);
			cv::waitKey(1000);
		}
	}

}

ImageRecorder::ImageRecorder() {
	using namespace std;

	cout << "Welcome to the Image Recorder!" << endl;

	cout << "First, enter the path to store the images in: " << endl;
	string path;
	cin >> path;

	while (!FileHelper::folderExists(path)) {
		cout
				<< "ERROR: The specified directory was not found. Enter another path:"
				<< endl;
		cin >> path;
	}

	cout << "Enter the name of the person who will be taken images of: "
			<< endl;
	string name;
	cin >> name;

	cout << "Initializing Camera..." << endl;
	Camera::getInstance(); //calling this function to initialize camera prior to opening UI.

	cout << "Press ESC to exit program" << endl;

	displayUI(name, path);
}

